﻿// ----------------------------------------------------------
// Copyright (C) vtc. All rights reserved.
// VTCUtils.js
// Author: Thần sầu
// Email: Vankhuyen.nguyen@vtc.vn
// ----------------------------------------------------------


if (typeof window.VTCNamespace == "undefined")
{
    window.VTCNamespace = {};
}

VTCNamespace.registerNamespace = function(ns) {
    var nsParts = ns.split(".");
    var root = window;
    for (var i = 0; i < nsParts.length; i++)
    {
        if (typeof root[nsParts[i]] == "undefined")
            root[nsParts[i]] = new Object();
        root = root[nsParts[i]];
    }
}

VTCNamespace.registerNamespace("VTC");

VTC.Browser = {};
VTC.Browser.InternetExplorer = {};
VTC.Browser.Firefox = {};
VTC.Browser.Safari = {};
VTC.Browser.Opera = {};
VTC.Browser.Chrome = {};
VTC.Browser.agent = null;
VTC.Browser.name = navigator.appName;
VTC.Browser.version = parseFloat(navigator.appVersion);
if (navigator.userAgent.indexOf(' MSIE ') > -1)
{
    VTC.Browser.agent = VTC.Browser.InternetExplorer;
    VTC.Browser.version = parseFloat(navigator.userAgent.match(/MSIE (\d+\.\d+)/)[1]);
}
else if (navigator.userAgent.indexOf('Firefox/index.html') > -1)
{
    VTC.Browser.agent = VTC.Browser.Firefox;
    VTC.Browser.version = parseFloat(navigator.userAgent.match(/ Firefox\/(\d+\.\d+)/)[1]);
    VTC.Browser.name = 'Firefox';
}
else if (navigator.userAgent.indexOf('Safari/index.html') > -1)
{
    VTC.Browser.agent = VTC.Browser.Safari;
    VTC.Browser.version = parseFloat(navigator.userAgent.match(/ Safari\/(\d+\.\d+)/)[1]);
    VTC.Browser.name = 'Safari';
}
else if (navigator.userAgent.indexOf('Opera/index.html') > -1)
{
    VTC.Browser.agent = VTC.Browser.Opera;
}

if (navigator.userAgent.indexOf('Chrome/index.html') > -1)
{
    VTC.Browser.agent = VTC.Browser.Chrome;
}
VTC.Browser.isIE = VTC.Browser.agent === VTC.Browser.InternetExplorer;
VTC.Browser.isFirefox = VTC.Browser.agent === VTC.Browser.Firefox;
VTC.Browser.isSafari = VTC.Browser.agent === VTC.Browser.Safari;
VTC.Browser.isOpera = VTC.Browser.agent === VTC.Browser.Opera;
VTC.Browser.isChrome = VTC.Browser.agent === VTC.Browser.Chrome;

VTC.Events = {};
VTC.Events.addListener = function(element, type, bubbling, expression) {
    bubbling = bubbling || false;	
    if(window.addEventListener)	{ 
        // Standard7.		
        element.addEventListener(type, expression, bubbling);
        return true;
    } else if(window.attachEvent) { 
        // IE10.		
        element.attachEvent('on' + type, expression);
        return true;
    } else return false;
};
VTC.Events.removeListener = function(element, type,bubbling, expression) {
    bubbling = bubbling || false;
    
    if(window.removeEventListener) { 
        // Standard7.		
        element.removeEventListener(type, expression, bubbling);
        return true;
    } else if(window.detachEvent) { 
        // IE10.		
        element.detachEvent('on' + type, expression);
        return true;
    } else return false;
};

// VTC.Utils namespace
VTCNamespace.registerNamespace("VTC.Utils");

// VTC.Utils.String inherit from String object
VTC.Utils.String = {};
VTC.Utils.String.prototype = new String();
VTC.Utils.String.removeWhiteSpace = function(textSource) { return textSource.replace(/\s+/g,''); };
VTC.Utils.String.Trim = function(textSource) { if (textSource) return textSource.replace(/^\s+|\s+$/,""); return ''; };
VTC.Utils.String.replaceAll = function(source, textReplace, newText) { 
    // Have some problems with special character as 'dot', 'square-brackets' etc.
    return source.replace(new RegExp(textReplace, 'g'), newText);     
};

VTC.Utils.Control = {};
VTC.Utils.Control.getElementById = function(id) { 
    if (typeof id == "undefined") return null;
    if (VTC.Utils.String.removeWhiteSpace(id).length <= 0) return null;
    
    return document.all ? document.all[id] : document.getElementById(id); 
};
//
// Get element by Id
// Usage: $control('element id')
//
var $control = function(id) { return VTC.Utils.Control.getElementById(id); };
//
// Get location (x, y) of a control in the page
//
VTC.Utils.Control.getLocation = function(element) {
    var location = new Object();
    location.x = getX();
    location.y = getY();
    
    return location;
    
    function getX()
    {
        var x = 0;
        for (var e = element; e; e = e.offsetParent)
            x += e.offsetLeft;
        for (var e = element; e && e != document.body; e = e.parentNode)
            if (e.scrollLeft) x -= e.scrollLeft;
            
        return x;
    }
    function getY()
    {
        var y = 0;
        for (var e = element; e; e = e.offsetParent)
            y += e.offsetTop;
        for (var e = element; e && e != document.body; e = e.parentNode)
            if (e.scrollTop) e -= e.scrollTop;
        
        return y;
    }
};
//
// Get location (x, y) of a control in the page
//
var $location = function(element) { return VTC.Utils.Control.getLocation(element); };


//
// Get page scroll position
//
VTC.Utils.Scroller = {
    Left : function () {
        var lf;
        lf = 0;
        if (typeof window.pageXOffset != 'undefined')
            lf = window.pageXOffset;
        else if (typeof document.documentElement.scrollLeft != 'undefined' && document.documentElement.scrollLeft > 0)
            lf = document.documentElement.scrollLeft;
        else if (document.body && typeof(document.body.scrollLeft) != 'undefined')
            lf = document.body.scrollLeft;
        return lf;
    },
    Top : function() {
        var tp;
        tp = 0;
        if (typeof window.pageYOffset != 'undefined')
            tp = window.pageYOffset;
        else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
            tp = document.documentElement.scrollTop;
        else if (document.body && typeof(document.body.scrollTop) != 'undefined')
            tp = document.body.scrollTop;
        return tp;
    }
}

var $scroller = VTC.Utils.Scroller;

//
// Get mouse position
//
VTC.Utils.Mouse = {
    x : 0,
    y : 0,
    initialize : function() {
        // If NS -- that is, !IE -- then set up for mouse capture
        if (!VTC.Browser.isIE) document.captureEvents(Event.MOUSEMOVE)

        // Set-up to use getMouseXY function onMouseMove
        VTC.Events.addListener(document, 'mousemove', false, this._getMouseXY);
        //document.onmousemove = this._getMouseXY;
        return this;
    },
    _getMouseXY : function(e) { 
        var tempX = 0;
        var tempY = 0;
        
        if (!e) var e = window.event;
        
        if (e.pageX || e.pageY) { // grab the x-y pos.s if browser is NS
            tempX = e.pageX;
            tempY = e.pageY;
        }
        else if (e.clientX || e.clientY) { // grab the x-y pos.s if browser is IE
            //var scr = new VTC.Utils.ScrollingPosition();
            
            tempX = window.event.clientX + $scroller.Left();
            tempY = window.event.clientY + $scroller.Top();
        }
        
        // catch possible negative values in NS4
        if (tempX < 0) { tempX = 0; }
        if (tempY < 0) { tempY = 0; }  
        // show the position values in the form named Show
        // in the text fields named MouseX and MouseY
        VTC.Utils.Mouse.x = tempX;
        VTC.Utils.Mouse.y = tempY;
    }
};
var $mouse = VTC.Utils.Mouse.initialize();

// 
// Some DateTime utilities
// 
VTC.Utils.DateTime = {
    add : function(date, days) {
        if (date instanceof Date) {
            date.setTime(date.getTime() + days * 86400000);
        }
        return date;
    },
    getDayName : function(date) {
        if (date instanceof Date) {
            var dayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday"];
            return dayNames[date.getDay()];
        }
        return '';
    },
    getMonthName : function(date) {
        if (date instanceof Date) {
            var monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"];
            return monthNames[date.getMonth()];
        }
        return '';
    }
};
